package com.Telien.app.ui.main;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import com.Telien.app.Telien;
import com.Telien.app.request.object.DriverRequest;
import com.Telien.app.request.service.RequestService;
import com.Telien.app.user.service.UserManagementService;



public class RequestSearchPanel extends JPanel implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel findLabel;
	private JTextField findTextField;
	private JButton enterButton;
	private RequestService requestService;
	private UserManagementService ums;
	Object dataValues[][];
	JTable table;
	
	public RequestSearchPanel(String type){
		FlowLayout experimentLayout = new FlowLayout();
		setLayout(experimentLayout);
		findLabel = new JLabel("Search:");
		findTextField = new JTextField(30);
		enterButton = new JButton("Enter");
		enterButton.addActionListener(this);
		add(findLabel);
		add(findTextField);
		add(enterButton);
		if(type.equals("driver")){
			initDriverRequest();
		}
		ums = new UserManagementService();
	}
	
	public RequestSearchPanel(String type, JTable table) {
		FlowLayout experimentLayout = new FlowLayout();
		setLayout(experimentLayout);
		findLabel = new JLabel("Search:");
		findTextField = new JTextField(30);
		enterButton = new JButton("Enter");
		enterButton.addActionListener(this);
		add(findLabel);
		add(findTextField);
		add(enterButton);
		if(type.equals("driver")){
			initDriverRequest();
		}
		ums = new UserManagementService();
	}

	public void initDriverRequest(){
		requestService = new RequestService();
		
	}
	
	private void searchTable(String searchterm){
		requestService = new RequestService();
		ArrayList<DriverRequest> requests = requestService.searchDriverRequest(searchterm);
		Object dataValues[][];
		ums.loadUsersFromDb();
		if (requests != null) {
			dataValues = new Object [requests.size()][6];
			if (!requests.isEmpty()) {
				for (int numRequests = 0; numRequests < requests.size(); numRequests++) {
						
						dataValues[numRequests][0] = ums.getUserName(requests.get(numRequests).getUserUUID());
						//dataValues[numRequests][0] = requests.get(numRequests).getUserUUID();
						dataValues[numRequests][1] = requests.get(numRequests).getPassanagerCount() + "";
						dataValues[numRequests][2] = requests.get(numRequests).getStartTime().toString();
						dataValues[numRequests][3] = requests.get(numRequests).getEndTime().toString();
						dataValues[numRequests][4] = requests.get(numRequests).getStartCord().toString();
						dataValues[numRequests][5] = requests.get(numRequests).getEndCord().toString();
					}
				}
			}
		DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
		tableModel.fireTableDataChanged();
		
	}
	
	public Object[][] setDataValues(){
		return dataValues;
	}
	
	public void setDataValues(Object[][] values){
		dataValues= values;
	}
	
	public void actionPerformed(ActionEvent e) {
		String name = e.getActionCommand();
		if (name.equals("Enter")) {
			System.out.println("Enter");
			String searchTerm = findTextField.getText();
			searchTable(searchTerm);
		}
		
	}
}
